package com.example.order_engine.interfaces.controller;

import com.example.order_engine.application.service.OrderEngineService;
import com.example.order_engine.domain.model.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 订单控制器
 */
@RestController
@RequestMapping("/api/orders")
public class OrderController {
    
    private final OrderEngineService orderEngineService;
    
    @Autowired
    public OrderController(OrderEngineService orderEngineService) {
        this.orderEngineService = orderEngineService;
    }
    
    @PostMapping
    public ResponseEntity<Order> createOrder(@RequestBody Map<String, Object> orderRequest) {
        try {
            Order order = orderEngineService.createOrder(orderRequest);
            return ResponseEntity.ok(order);
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().build();
        } catch (Exception e) {
            return ResponseEntity.internalServerError().build();
        }
    }
    
    @GetMapping("/health")
    public ResponseEntity<String> health() {
        return ResponseEntity.ok("Order Engine is running");
    }
}